﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

using Microsoft.Practices.EnterpriseLibrary.Security.Cryptography;
using PetaPoco;
using Marking.Dashboard.Web.ServiceContracts;

namespace Marking.Dashboard.Web.Services
{
    public class LoginService : ILoginService
    {
        public bool Login(string password)
        {
            var users = this.CreateDb().Fetch<User>("SELECT * FROM SecUsers");

            return users.Any(x =>
            {
                return CompareHash(password, x.Password);
            });
        }

        private Database CreateDb()
        {
            return new Database("Marking");
        }

        public static bool CompareHash(string plainPassword, string hashedPassword)
        {
            return Cryptographer.CompareHash("MD5", plainPassword, hashedPassword);
        }

        private class User
        {
            public string Password { get; set; }
        }
    }
}